home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / WLIB.ZIP / WLINK.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-01  |  5.5 KB  |  140 lines

  1. #ifndef WLinkIncluded
  2. #define WLinkIncluded
  3.  
  4. // copyright (c) 1992, 1993 by Paul Wheaton
  5. // 1916 Brooks #205, Missoula, MT  59801
  6. //
  7. //       phone:  (406)543-1928
  8. //  CompuServe:  72707,207
  9. //    Internet:  72707.207@CompuServe.com
  10.  
  11. #include <stddef.h>
  12. #include <WMisc.h>
  13.  
  14. #ifdef MAJORBBS
  15.   #include <stdlib.h>
  16. #endif
  17.  
  18. struct LinkedListElement
  19.   {
  20.     friend class LinkedList;
  21.     LinkedListElement *PrevRec, *NextRec;
  22.     void* Rec;
  23.   };
  24.  
  25. class LinkedList
  26.   {
  27.       LinkedListElement *RootRec, *CurRec;
  28.       Long NumRecs;
  29.       Long CurRecNum; // 0...NumRecs-1
  30.     public:
  31.       LinkedList();
  32.         // an empty form
  33.       ~LinkedList();
  34.       void* Root();
  35.         // returns rec 0.  CurRec is set to RootRec.
  36.       void* Cur() {return CurRec->Rec;}
  37.       Long Size() {return NumRecs;}
  38.         // the number of things in list
  39.       Long Top() {return(NumRecs-1);}
  40.       Long Num() {return CurRecNum;}
  41.         // 0..Top():  Cur's index
  42.       void* Next();
  43.         // Cur is moved up and returned
  44.       void* Prev();
  45.         // Cur is moved back and returned
  46.       void* Last();
  47.         // like Root only it's the last element
  48.       Bool Insert(void* NewRec);
  49.         // will be inserted between Prev and Cur
  50.       Bool Add(void* NewRec);
  51.         // will be inserted between Cur and Next
  52.       Bool Append(void* NewRec);
  53.         // will be the last record in the list
  54.       Bool Push(void* NewRec){return Append(NewRec);}
  55.       void DelCur();
  56.         // link is destroyed.  Cur will be old Next.  Object is not deleted
  57.       #ifndef MAJORBBS
  58.       void DelCurObj()  // same as DelCur cept object is destroyed too
  59.         // Note!  Only the object memory is freed.  No object destructors are called
  60.         {delete Cur(); DelCur();}
  61.       #endif
  62.       void* Pop();
  63.         // Returns pointer to last rec. Link is destroyed. Cur will be old Prev.
  64.       void* operator[](Long Index);
  65.  
  66.     //  to do:  add in stuff like Add(void* NewRec,...);
  67.  
  68.       #ifdef MAJORBBS
  69.         void* operator new(size_t size){return malloc(size);}
  70.         void  operator delete(void* p) {free(p);}
  71.       #endif
  72.   };
  73.  
  74. #ifdef MAJORBBS
  75.  
  76.   #define CreateLinkedListClass(ClassName,ObjType)                        \
  77.                                                                           \
  78.   class ClassName: public LinkedList                                      \
  79.     {                                                                     \
  80.       public:                                                             \
  81.         ObjType& Root(){return *(ObjType*)LinkedList::Root();}            \
  82.         ObjType& Last(){return *(ObjType*)LinkedList::Last();}            \
  83.         ObjType& Cur() {return *(ObjType*)LinkedList::Cur();}             \
  84.         ObjType& Next(){return *(ObjType*)LinkedList::Next();}            \
  85.         ObjType& Prev(){return *(ObjType*)LinkedList::Prev();}            \
  86.         ObjType& Pop() {return *(ObjType*)LinkedList::Pop();}             \
  87.         Bool Insert(ObjType& NewRec){return LinkedList::Insert(&NewRec);} \
  88.         Bool Add(ObjType& NewRec)   {return LinkedList::Add(&NewRec);}    \
  89.         Bool Append(ObjType& NewRec){return LinkedList::Append(&NewRec);} \
  90.         Bool Push(ObjType& NewRec)  {return LinkedList::Push(&NewRec);}   \
  91.         ObjType& operator[](Long Index)                                   \
  92.           {return *(ObjType*)(LinkedList::operator[](Index));}            \
  93.         void DelAllObj(); /* {while (Size()) DelCurObj();} */             \
  94.     };
  95.  
  96. #else
  97.  
  98.   #define CreateLinkedListClass(ClassName,ObjType)                        \
  99.                                                                           \
  100.   class ClassName: public LinkedList                                      \
  101.     {                                                                     \
  102.       public:                                                             \
  103.         ObjType& Root(){return *(ObjType*)LinkedList::Root();}            \
  104.         ObjType& Last(){return *(ObjType*)LinkedList::Last();}            \
  105.         ObjType& Cur() {return *(ObjType*)LinkedList::Cur();}             \
  106.         ObjType& Next(){return *(ObjType*)LinkedList::Next();}            \
  107.         ObjType& Prev(){return *(ObjType*)LinkedList::Prev();}            \
  108.         ObjType& Pop() {return *(ObjType*)LinkedList::Pop();}             \
  109.         Bool Insert(ObjType& NewRec){return LinkedList::Insert(&NewRec);} \
  110.         Bool Add(ObjType& NewRec)   {return LinkedList::Add(&NewRec);}    \
  111.         Bool Append(ObjType& NewRec){return LinkedList::Append(&NewRec);} \
  112.         Bool Push(ObjType& NewRec)  {return LinkedList::Push(&NewRec);}   \
  113.         Bool Insert(ObjType* NewRec){return LinkedList::Insert(NewRec);} \
  114.         Bool Add(ObjType* NewRec)   {return LinkedList::Add(NewRec);}    \
  115.         Bool Append(ObjType* NewRec){return LinkedList::Append(NewRec);} \
  116.         Bool Push(ObjType* NewRec)  {return LinkedList::Push(NewRec);}   \
  117.         ObjType& operator[](Long Index)                                   \
  118.           {return *(ObjType*)(LinkedList::operator[](Index));}            \
  119.         void DelCurObj()                                                  \
  120.           {delete ((ObjType*)LinkedList::Cur()); LinkedList::DelCur();}   \
  121.         void DelAllObj(); /* {while (Size()) DelCurObj();} */             \
  122.     };
  123.  
  124. #endif
  125.  
  126. class Queue:public LinkedList
  127.   {
  128.     public:
  129.       Queue(){}
  130.       void* Pop();
  131.   };
  132.  
  133. class Stack:public LinkedList
  134.   {
  135.     public:
  136.       Stack(){}
  137.   };
  138.  
  139. #endif
  140.